package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import util.Util;
import bean.Customer;
import bean.FundTransaction;
import bean.LoggedUser;
import bean.Model;
import bean.TransactionStatus;
import bean.TransactionType;
import bean.UserType;
import dao.CustomerDAO;
import dao.FundTransactionDAO;

/*
 * 
 * Created By Sahil Thakkar for Request Check Usecase
 */

public class RequestCheckAction extends Action {

	String strLong	= null;
	private CustomerDAO customerDAO = null;
	private FundTransactionDAO fundTransactionDAO = null;
	private FundTransaction fundTransaction = null;
	Double l = 0.0;
	
	public RequestCheckAction(Model model) {
		customerDAO = model.getCustomerDAO();
		fundTransactionDAO = model.getFundTransactionDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "requestCheck.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		try {
			HttpSession session     = request.getSession(false);
			LoggedUser user = (LoggedUser) session.getAttribute("user");
			List<String> errors = new ArrayList<String>();

			strLong = request.getParameter("amount");
			if(Util.isValidMoneyAmount(strLong))
			{

				try{
					l = Double.parseDouble(strLong);
					l = l*100;
				}
				catch(Exception e){
					errors.add("Please enter a valid amount in digits");
				}
				if(l>0)
				{
					Customer c = customerDAO.getCustomerById(""+user.getId());
					if(c.getCash() < l)
					{
						errors.add("Requested amount is greater than the available balance. Please try again");
					}
					else
					{
						fundTransaction = new FundTransaction();
						fundTransaction.setCustomer(c);
						fundTransaction.setOrderDate(new Date());
						fundTransaction.setTransactionType(TransactionType.REQUEST_CHECK);
						fundTransaction.setAmount(Util.convertToDBMoney(strLong));
						fundTransaction.setStatus(TransactionStatus.Pending);
						fundTransactionDAO.createFundTransaction(fundTransaction);
					}
				}
				else
					errors.add("Please enter a valid amount greater than equal to 0");				
			}
			else
			{
				errors.add("The request check amount should be less than 1,000,000 and less than two decimal points");
			}

			if (errors.size() > 0) {
				request.setAttribute("errors", errors);
				return "openRequestCheck.do";
			}
			
				request.setAttribute("message", "You have successfully requested check for " +strLong+" amount." );
				return "employeeConsole.jsp";
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error.jsp";
		}
	}

	@Override
	public UserType getType() {
		// TODO Auto-generated method stub
		return UserType.Customer;
	}
}
